﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace IronWiki
{
    [Description("A class that provides data about the current HttpRequest")]
    public class WikiRequest
    {
        [Description("The Url in a string for the current request")]
        public string Url { get; set; }
        [Description("The IP of the current request")]
        public string VisitorIdentity { get; set; }
        [Description("An array of parameter names for query, form, cookie and server variable items")]
        public string[] ParameterNames { get; set; }
        [Description("A value indicating if the user is authenticated")]
        public bool IsAuthenticated { get; set; }
        [Description("A value giving the user name when authenticated")]
        public string AuthenticatedUserName { get; set; }

        private HttpRequestBase _request;

        public WikiRequest()
        {
            _request = new HttpRequestWrapper(HttpContext.Current.Request);
            Init(_request);
        }

        public WikiRequest(HttpRequestBase request)
        {
            _request = request;
            Init(_request);
        }

        public void Init(HttpRequestBase _request)
        {
            Url = _request.Url.ToString();
            VisitorIdentity = _request.UserHostAddress;
            IsAuthenticated = _request.IsAuthenticated;
            if (IsAuthenticated) {
                AuthenticatedUserName = _request.RequestContext.HttpContext.User.Identity.Name;
            }
            ParameterNames = _request.Params.AllKeys;
        }

        [Description("returns the parameter requested by name")]
        public string GetParameterNamed(string parm)
        {
            return _request[parm];
        }

        [Description("Returns a boolean indicating if the paramater exists")]
        public bool ParameterExists(string parm)
        {
            return _request.Params.AllKeys.Contains(parm);
        }

        [Description("returns the cookie requested by name")]
        public string GetCookie(string key)
        {
            return _request.Cookies[key].Value;
        }
    }
}
